package io.renren.modules.web;
 

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.renren.common.utils.CommonUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.mem_token.service.V9MemTokenService;
import io.renren.modules.member.entity.V9MemberEntity;
import io.renren.modules.member.service.V9MemberService;
import io.renren.modules.news.entity.V9NewsEntity;
import io.renren.modules.news.service.V9NewsService;
import io.renren.modules.news_data.entity.V9NewsDataEntity;
import io.renren.modules.news_data.service.V9NewsDataService;

@Controller
public class IndexController {
	@Autowired
	private V9MemberService v9MemberService;
	@Autowired
	private V9MemTokenService v9MemTokenService;
	@Autowired
	private V9NewsService v9NewsService;
	@Autowired V9NewsDataService v9NewsDataService;

	@RequestMapping("/index")
	public String index(HttpServletResponse response, ModelMap map) {
		map.addAttribute("name", "haozz");
		return "index/index";
	}

	@RequestMapping("/")
	String home() {
		return "index/index";
	}

	@RequestMapping("/index/wuliu")
	String wuliu() {
		return "index/wuliu";
	}

	@RequestMapping("/index/news")
	String news() {
		return "index/news";
	}

	@RequestMapping("/index/yunfei")
	String yunfei() {
		return "index/yunfei";
	}

	@RequestMapping("/index/question")
	String question() {
		return "index/question";
	}

	@ResponseBody
	@RequestMapping("/index/login")
	public R login(@RequestBody JSONObject jsonParam) {
		String email = jsonParam.getString("email");
		String password = jsonParam.getString("password");
		V9MemberEntity user = v9MemberService.getOne(new QueryWrapper<V9MemberEntity>().eq("email", email));

		// 账号不存在、密码错误
		if (user == null) {
			return R.error("账号不存在");
		}
		String encrypt = user.getEncrypt();
		String pass = user.getPassword();
		String newpass = CommonUtils.encrypt(password, encrypt);
		if (!newpass.equals(pass)) {
			return R.error("账号或密码错误");
		}

		// 账号锁定
		if (user.getIslock() != 0) {
			return R.error("账号已被锁定,请联系管理员");
		}
		if (user.getLahei() != 0) {
			return R.error("账号已被拉黑,请联系管理员");
		}
		if (user.getFenghao() != 0) {
			return R.error("账号已被封,请联系管理员");
		}
		// 生成token，并保存到数据库
		R r = v9MemTokenService.createToken(user.getUserid());
		r.put("username", user.getUsername());
		return r;

	}

	/**
	 * 新闻列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/index/listNews")
	public R listNews(@RequestParam Map<String, Object> params) {
		 
		PageUtils page = v9NewsService.getNewsList(params);
		return R.ok().put("page", page);
	}
	@ResponseBody
	@RequestMapping("/index/listInfo")
	public R listInfo(Integer id) {
		V9NewsEntity news = v9NewsService.getById(id);
		V9NewsDataEntity v9NewsData = v9NewsDataService.getOne(new QueryWrapper<V9NewsDataEntity>().eq("id", id));
        return R.ok().put("content", v9NewsData).put("news", news);
	}
	 

	
	
}
